from solver import Solver


class GreedySolver(Solver):
    def __init__(self, data_path='data.txt'):
        super().__init__(data_path=data_path)

    def _put_step_into_machine(self, time_table, component_idx, component_step_idx, time_period, now_time):
        if len(time_table) == 0:
            time_table.append((component_idx, component_step_idx, now_time, now_time + time_period))
            return now_time + time_period
        for i in range(len(time_table)):
            if now_time + time_period <= time_table[i][2]:
                time_table.insert(i, (component_idx, component_step_idx, now_time, now_time + time_period))
                return now_time + time_period
            now_time = max(now_time, time_table[i][3])
        time_table.insert(len(time_table), (component_idx, component_step_idx, now_time, now_time + time_period))
        return now_time + time_period

    def solve(self):
        return self.simple_greedy_by_order()

    # 简单顺序贪心 只考虑k=1的情况
    def simple_greedy_by_order(self):
        n = self.data.n
        m = self.data.m
        a = self.data.a
        sol = [list() for _ in range(m)]
        for i in range(n):
            now = 0
            for j in range(len(a[i])):
                machine_idx = a[i][j][0][0]
                time = a[i][j][0][1]
                now = self._put_step_into_machine(sol[machine_idx], i, j, time, now)
        return sol
